/**
 *
 * @author ace
 * @create 2017/7/27
 */
class DaikaiItem extends ComponentEx {

    private lab_rank: eui.Label;
    private lab_roomid: eui.Label;
    private lab_card: eui.Label;
    private btn_rule: ButtonEx;
    private lab_time: eui.Label;
    private cellGroup: eui.Group;
    private item_1: DaikaiPlayerItem;
    private item_2: DaikaiPlayerItem;
    private item_3: DaikaiPlayerItem;
    private item_4: DaikaiPlayerItem;
    private btnGroup: eui.Group;
    private btn_quit: ButtonEx;
    private btn_invite: ButtonEx;
    private btn_details: ButtonEx;
    private btn_play: ButtonEx;
    private btn_share: ButtonEx;
    private img_yikai: eui.Image;

    private tips: TextTips;

    constructor() {
        super();
        this.skinName = "DaikaiItemSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.img_yikai.visible = false;

        this.tips = new TextTips(this.btn_rule);

        this.btn_details.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_invite.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_play.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_quit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_share.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {

        if (!this.data) return;

        switch (e.currentTarget) {
            case this.btn_quit:
                let thiz = this;
                game.askPanel.showMsg(function (r) {
                    if (r) {
                        gameManager.socketManager.send(34, { roomid: thiz.data.roomid, type: GameConfig.gameType });
                    }
                }, '游戏未开始，解散房间不扣房卡，\n是否解散？', null, null, false, true);
                break;
            case this.btn_invite:
                game.roomid = this.data.roomid;
                game.roomRoundType = this.data.round_type;
                game.roomRound = this.data.round;
                game.roomSeat = this.data.renshu;
                game.roomRules = this.data.rules;
                game.roomCreateTime = this.data.create_time;

                GameConfig.initShare(true);
                game.share.show();
                break;
            case this.btn_details:
                game.roomid = this.data.roomid;
                game.recordInfos = this.data.persons;

                Replayer.shareIF.item_id = this.data.item_id.split("#")[1];

                gameManager.socketManager.send(20, { id: this.data.id });

                let panel: DaikaiPanel = gameManager.find('DaikaiPanel');
                if (panel) {
                    panel.updateRecordDetailsInfo(this.data.rank, this.data.roomid, this.timeStr)
                }

                gameManager.i.dispatchEvent(GameEventType.UPDATE_RECORD_DETAILS_TYPE, 1);
                break;
            case this.btn_play:
                game.ReplayType = ReplayType.daikai;
                gameManager.socketManager.send(21, { id: this.data[1] });
                break;
            case this.btn_share:
                for (let i: number = 0; i < game.recordInfos.length; i++) {
                    if (game.recordInfos[i].pos == 1) {
                        Replayer.shareIF.uid = game.recordInfos[i].uid;
                    }
                }
                Replayer.shareIF.playback_id = this.data[1].split("#")[1];
                Replayer.shareIF.share_desc = "来自:" + game.player.nick + "   时间:" + this.timeStr;
                Weixin.shareReplay();
                break;
        }
    }

    update(state: string, data: any) {
        this.skinState = state;

        if (!data) return;

        this.data = data;

        switch (state) {
            case 'list':
                this.refresh_list();
                break;
            case 'record':
                this.refresh_record();
                break;
            case 'details':
                this.refresh_details();
                break;
        }
    }

    refresh_list() {
        this.lab_roomid.text = '' + this.data.roomid;
        this.lab_card.text = '' + this.data.cost;
        this.lab_time.text = '' + this.timeStr;

        this.tips.desc = this.data.round + (this.data.round_type == RoundType.quan ? '圈' : '局') +
            ' ' + game.ruleTexts(this.data.rules);

        let item: DaikaiPlayerItem;
        let info: any;
        for (let i: number = 1; i <= 4; i++) {
            item = this['item_' + i];

            info = this.data.info[i - 1];

            if (info) {
                // info.status = this.data.status;
                info.roomid = this.data.roomid;

                item.update(info);
            }
            else {
                i <= +this.data.renshu ? item.empty() : item.lock();
            }
        }

        this.starting = this.data.status == 'start';
    }

    refresh_record() {
        this.lab_roomid.text = '' + this.data.roomid;
        this.lab_card.text = '' + this.data.cost;
        this.lab_time.text = '' + this.timeStr;

        this.tips.desc = this.data.round + (this.data.round_type == RoundType.quan ? '圈' : '局') + ' ' +
            game.ruleTexts(this.data.rules);

        let item: DaikaiPlayerItem;
        let info: any;
        for (let i: number = 1; i <= 4; i++) {
            item = this['item_' + i];

            info = this.data.persons[i - 1];

            if (info) {
                info.status = this.data.status;
                info.roomid = this.data.roomid;
                info.cur = this.data.total[info.pos - 1];
                item.update(info);
            }
            else {
                item.lock();
            }
        }
    }

    refresh_details() {
        this.lab_time.text = '' + this.timeStr;

        let item: DaikaiPlayerItem;
        let info: any;
        for (let i: number = 1; i <= 4; i++) {
            item = this['item_' + i];

            info = game.recordInfos[i - 1];

            if (info) {
                info.status = this.data.status;
                info.cur = this.data[info.pos + 1];
                item.update(info);
            }
            else {
                item.lock();
            }
        }
    }

    set starting(v: boolean) {
        if (!this.data) return;

        this.img_yikai.visible = v;
        this.btnGroup.visible = !v;
    }

    set rank(va: number) {
        if (this.skinState == 'details') {
            this.lab_rank.text = '第' + va + '局';
        }
        else {
            this.lab_rank.text = '' + va;
        }

        this.data.rank = va;
    }

    private get timeStr() {
        if (this.skinState == 'list') {
            return StringUtils.getYTDByTimestamp(this.data.create_time * 1000) + ' ' + StringUtils.getHMSByTimestamp(this.data.create_time * 1000);

        }
        else if (this.skinState == 'record') {
            return StringUtils.getYTDByTimestamp(this.data.time * 1000) + ' ' + StringUtils.getHMSByTimestamp(this.data.time * 1000);
        }
        else if (this.skinState == 'details') {
            return StringUtils.getYTDByTimestamp(this.data[0] * 1000) + " " + StringUtils.getHMSByTimestamp(this.data[0] * 1000);
        }
    }
}